#!/usr/bin python
# -*- coding: utf-8 -*-
from datetime import datetime
from ConfigParser import SafeConfigParser, NoOptionError, ParsingError
from sqlobject import *

# =====================================================================================
# ============= nacteni udaju z ini souboru ===========================================
# =====================================================================================
#nazevIniSouboru = '/automat/konfigurace.ini'
#
#confDict = SafeConfigParser()
#confDict.read(nazevIniSouboru)

from nacteniKonfigurace import adresarRAMDisk, adresarDatabazeAutomatu, db_filename, tmp_db_filename, adresarRAMDisk

# =====================================================================================
# ============= definice ulozeni databazovych souboru SQLite ==========================
# =====================================================================================
connectionRAMdisk = connectionForURI('sqlite://%s%s' % (adresarRAMDisk, tmp_db_filename), timeout = 10*1000) # timeout je v milisekundach
connectionInMemory = connectionForURI('sqlite:/:memory:')

# =====================================================================================
# ============= definice ulozeni databazovych souboru Firebird ========================
# =====================================================================================
connection = connectionForURI('firebird://sysdba:automati@127.0.0.1:3050/%s%s' % (adresarDatabazeAutomatu, db_filename))


class SmsProOdeslani(SQLObject):
    _connection = connection
    class sqlmeta:
        cacheValues = False
    tStamp = DateTimeCol(default=datetime.now)
    textZpravy = StringCol(length = 2000)
    odeslana = BoolCol(default=False)    

class UdalostiAutomatu(SQLObject):
    _connection = connection
    class sqlmeta:
        cacheValues = False
    tStamp = DateTimeCol(default=datetime.now)
    popisUdalosti = StringCol(length=200)
    periferie = StringCol(length=30)
    priznakExportovano = BoolCol(default=False)
    priznakExportovano.noCSV = True    

def vytvorTabulky():    
    UdalostiAutomatu.createTable(ifNotExists=True)    
    SmsProOdeslani.createTable(ifNotExists=True)    

seznamTabulek=(UdalostiAutomatu, )

def vymazZaznamyTabulek():    
    for tabulka in seznamTabulek:
        for radek in tabulka.selectBy(priznakExportovano = True):
#        for radek in tabulka.select():
            radek.destroySelf()

def vypisObsahTabulky(tabulka=UdalostiAutomatu):
    for radek in tabulka.selectBy(priznakExportovano = False).orderBy("tStamp"):
        print radek.tStamp,  radek.popisUdalosti, radek.periferie 

vypisObsahTabulky()
#vymazZaznamyTabulek()
